package com.shanghai.market.plshnaghai.net;

import java.io.ByteArrayOutputStream;

import android.content.Context;
import android.graphics.Bitmap;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.shanghai.market.plshnaghai.AppApplication;
import com.shanghai.market.plshnaghai.entity.AppData;
import com.shanghai.market.plshnaghai.listeners.OnStringResultListener;
import com.shanghai.market.plshnaghai.publics.LogCat;

/**
 * 业务基类,直接返回整个字符串
 * 
 */
public class ImageManager {
	private OnStringResultListener onResultListener;
	private static final int VOLLEY_TIMEOUT = 50 * 1000;

	public ImageManager(Context context) {
		super();

	}

	public void setOnResultListener(OnStringResultListener onResultListener) {
		this.onResultListener = onResultListener;
	}

	public void executeRequest(int method, String url, Bitmap bmp) {
		LogCat.i("url:" + url);
		if (!AppApplication.getInstance().isNetConnected()) {
			handleFailResult("网络未连接!");
			return;
		}

		MultipartRequest multipartRequest = new MultipartRequest(url, responseListener(), errorListener());

		// 添加header
		multipartRequest.addHeader("header-name", "value");
		final AppData data = AppApplication.getInstance().getAppData();
		final String token = data.getToken();
		final String cookie = data.getCookie();
		multipartRequest.addHeader("Cookie", cookie);
		multipartRequest.addHeader("token", token);
		multipartRequest.addHeader("appVersion", "123456");
		multipartRequest.addHeader("deviceSn", "123456");

		// 通过MultipartEntity来设置参数
		MultipartEntity multi = multipartRequest.getMultiPartEntity();
		// 二进制参数
		multi.addBinaryPart("images", bitmapToBytes(bmp));

		multipartRequest.setRetryPolicy(new DefaultRetryPolicy(VOLLEY_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
				DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		RequestManager.addRequest(multipartRequest, this);
	}

	public void cancelAll() {
		RequestManager.cancelAll(this);
	}

	private void handleSuccessResult(String response) {
		if (this.onResultListener != null) {
			this.onResultListener.onSuccess(response);
		}
	}

	private void handleFailResult(String text) {
		if (this.onResultListener != null) {
			this.onResultListener.onFail(text);
		}
	}

	private ErrorListener errorListener() {
		return new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				final String errorData = error.getMessage();
				LogCat.i("response:" + errorData);
				handleFailResult("连接服务器异常!");
			}
		};
	}

	private Listener<String> responseListener() {
		final Listener<String> listener = new Listener<String>() {

			@Override
			public void onResponse(String result) {
				handleSuccessResult(result);
			}

		};
		return listener;
	}

	/**
	 * Bitmap转byte
	 * 
	 * @param Bitmap
	 * @return
	 */
	public byte[] bitmapToBytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
}
